package com.skyrin.draglayoutdemo;

import android.graphics.Outline;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.TextView;

import java.util.Locale;

public class ElevationDragFragment extends Fragment {
    public static final String TAG = "ElevationDragFragment";

    /* The circular outline provider */
    private ViewOutlineProvider mOutlineProviderCircle;

    /* The current elevation of the floating view. */
    private float mElevation = 0;

    /* The step in elevation when changing the Z value */
    private int mElevationStep;

    private GridLayout gd_users;
    private TextView tv_area;

    int tv_area_high;

    boolean viewInArea = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOutlineProviderCircle = new CircleOutlineProvider();
        mElevationStep = getResources().getDimensionPixelSize(R.dimen.elevation_step);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frg_main, container, false);

        /* Find the {@link View} to apply z-translation to. */
        final View floatingShape = rootView.findViewById(R.id.circle);

        final View sss = rootView.findViewById(R.id.sss);
        gd_users = rootView.findViewById(R.id.gd_users);
        tv_area = rootView.findViewById(R.id.tv_area);
        tv_area_high = dp2px(56);

        /* Define the shape of the {@link View}'s shadow by setting one of the {@link Outline}s. */
        floatingShape.setOutlineProvider(mOutlineProviderCircle);
        sss.setOutlineProvider(mOutlineProviderCircle);

        /* Clip the {@link View} with its outline. */
        floatingShape.setClipToOutline(true);

        DragFrameLayout dragLayout = rootView.findViewById(R.id.main_layout);

        dragLayout.setDragFrameController(new DragFrameLayout.DragFrameLayoutController() {

            @Override
            public void onDragDrop(View view, boolean captured) {
                /* Animate the translation of the {@link View}. Note that the translation
                 is being modified, not the elevation. */
                if (view.getId()==floatingShape.getId()){
                    floatingShape.animate()
                            .translationZ(captured ? 50 : 0)
                            .setDuration(100);
                    Log.d(TAG, captured ? "Drag" : "Drop");
                }

                if (view.getId()==sss.getId()){
                    sss.setClipToOutline(captured);
                }

                toggleArea(captured);

                Log.d(TAG, "X:"+view.getX()+" Y:"+view.getY());

                if (!captured&&viewInArea){
                    ((DragFrameLayout)view.getParent()).removeView(view);
                    gd_users.addView(view);
                }
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                viewInArea = changedView.getY()< tv_area_high;
            }
        });

        dragLayout.addDragView(floatingShape);
        dragLayout.addDragView(sss);

        /* Raise the circle in z when the "z+" button is clicked. */
        rootView.findViewById(R.id.raise_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mElevation += mElevationStep;
                Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
                floatingShape.setElevation(mElevation);
            }
        });

        /* Lower the circle in z when the "z-" button is clicked. */
        rootView.findViewById(R.id.lower_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mElevation -= mElevationStep;
                // Don't allow for negative values of Z.
                if (mElevation < 0) {
                    mElevation = 0;
                }
                Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
                floatingShape.setElevation(mElevation);
            }
        });

        return rootView;
    }

    /**
     * view 是否处于某个指定区域
     * @return
     */
    private boolean inArea(View view) {
//        view.getX();
        return false;
    }

    void toggleArea(boolean b){
        if (b){
            tv_area.setBackgroundColor(getResources().getColor(R.color.haf_trans));
        }else {
            tv_area.setBackgroundColor(getResources().getColor(R.color.full_trans));
        }
    }

    /**
     * ViewOutlineProvider which sets the outline to be an oval which fits the view bounds.
     */
    private class CircleOutlineProvider extends ViewOutlineProvider {
        @Override
        public void getOutline(View view, Outline outline) {
            outline.setOval(0, 0, view.getWidth(), view.getHeight());
        }
    }

    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public int dp2px(final float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * Value of px to value of dp.
     *
     * @param pxValue The value of px.
     * @return value of dp
     */
    public int px2dp(final float pxValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
